﻿namespace Website.Controllers
{
    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;

    using Wiki.IO;
    using Wiki.Web;
    using Wiki.Web.Mvc;

    using Website.Models;

    public class PageTitleController : Controller
    {
        [Http(Allow = "DELETE, GET, HEAD, OPTIONS, PUT")]
        public ActionResult ContentNegotiation(string title)
        {
            Uri uri = null;

            if (this.Request.Accepts("application", "atom+xml", false))
            {
                uri = this.RouteUri("page/{title}.atom", new RouteValueDictionary(new { title = title }));
            }
            else if (this.Request.Accepts("application", "xhtml+xml", false))
            {
                uri = this.RouteUri("page/{title}.xhtml", new RouteValueDictionary(new { title = title }));
            }
            else if (this.Request.Accepts("text", "plain", false))
            {
                uri = this.RouteUri("page/{title}.txt", new RouteValueDictionary(new { title = title }));
            }
            else
            {
                uri = this.RouteUri("page/{title}.html", new RouteValueDictionary(new { title = title }));
            }

            return new SeeOtherActionResult(uri);
        }

        [Http(Allow = "DELETE, GET, HEAD, OPTIONS, PUT")]
        public ActionResult Delete(string title)
        {
            WikiPage obj = WikiPage.Query(title);

            if (obj.IsAvailable)
            {
                obj.Remove();
                return new NoContentActionResult();
            }
            else
            {
                return new NotFoundActionResult();
            }
        }

        [Http(Allow = "DELETE, GET, HEAD, OPTIONS, PUT", SupportedMediaType = "text/plain")]
        public ActionResult Put(string title)
        {
            bool exists = WikiPage.Exists(title);
            
            WikiPage.Edit(title, this.Request.InputStream.ReadString());
            if (exists)
            {
                return new NoContentActionResult();
            }
            else
            {
                return new CreatedActionResult();
            }
        }

        [Http(Allow = "GET, HEAD, OPTIONS", ContentLanguage = "en", ContentLocation = "page", ContentType = "application/atom+xml")]
        public ActionResult AtomRepresentation(string title)
        {
            return this.View("AtomRepresentation", WikiPage.Query(title));
        }

        [Http(Allow = "GET, HEAD, OPTIONS", ContentLanguage = "en", ContentLocation = "page", ContentType = "text/plain")]
        public ActionResult TextRepresentation(string title)
        {
            return this.View("TextRepresentation", WikiPage.Query(title));
        }

        [Http(Allow = "GET, HEAD, OPTIONS", ContentLanguage = "en", ContentLocation = "page", ContentType = "text/html")]
        public ActionResult HtmlRepresentation(string title)
        {
            return this.View("HtmlRepresentation", WikiPage.Query(title));
        }

        [Http(Allow = "GET, HEAD, OPTIONS", ContentLanguage = "en", ContentLocation = "page", ContentType = "application/xhtml+xml")]
        public ActionResult XhtmlRepresentation(string title)
        {
            return this.View("XhtmlRepresentation", WikiPage.Query(title));
        }
    }
}